package com.bistu.xiong.service.impl;

import com.bistu.xiong.common.Constance;
import com.bistu.xiong.common.ServerResponse;
import com.bistu.xiong.dao.TulingUserMapper;
import com.bistu.xiong.pojo.TulingUser;
import com.bistu.xiong.service.IUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

import static com.bistu.xiong.common.Constance.*;
/**
 * Created by XiongXiaoJun on 2019/4/10.
 */
//对应接口IUserService
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private TulingUserMapper userMapper;

    public ServerResponse<TulingUser> register(TulingUser user){
        ServerResponse validResponse = this.checkValid(user.getUsername(),USERNAME);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkValid(user.getPhone(),PHONE);
        if(!validResponse.isSuccess()){
            return validResponse;
        }

        //MD加密
        user.setPassword(DigestUtils.md5Hex(user.getPassword()+password_salt));
        //初始化userId
        user.setUserId(getUUID());
        //随机生成token
        String token = DigestUtils.md5Hex(System.currentTimeMillis()+user.getUsername());
        user.setToken(token);
        if(userMapper.insert(user) == 0){
            return ServerResponse.createByErrorMsg("注册失败");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("注册成功",user);
    }

    @Override
    public ServerResponse<TulingUser> login(String phone, String password) {
        if (StringUtils.isBlank(phone)||StringUtils.isBlank(password))
            return ServerResponse.createByErrorCodeMsg(ERROR_PARAMETERS,"信息不全！");
        if(userMapper.checkPhone(phone) == 0){
            return ServerResponse.createByErrorCodeMsg(ERROR_NOT_FOUND_USER,"不存在该用户名");
        }
        // 密码登录MD5
        String md5Password = DigestUtils.md5Hex(password+password_salt);
        TulingUser user = userMapper.selectLogin(phone,md5Password);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ERROR_USER_PASSWORD,"密码错误");
        }
        user.setPassword(StringUtils.EMPTY);
        //随机生成token
        String token = DigestUtils.md5Hex(System.currentTimeMillis()+user.getUsername());
        user.setToken(token);
        if (userMapper.updateTokenByUserId(token,user.getUserId())==0){
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess("登录成功",user);
    }

    public ServerResponse<String> checkValid(String str,String type){
        if(StringUtils.isNotBlank(type)){
            //开始校验
            if(Constance.USERNAME.equals(type)){
                if(userMapper.checkUsername(str) > 0)
                    return ServerResponse.createByErrorCodeMsg(ERROR_EXIST_NAME,"用户名已存在");
            }
            if(Constance.PHONE.equals(type)){
                if(userMapper.checkPhone(str)>0){
                    return ServerResponse.createByErrorCodeMsg(ERROR_EXIST_PHONE,"手机已存在");
                }
            }
        }else{
            return ServerResponse.createByErrorCodeMsg(ERROR_PARAMETERS,"参数错误");
        }
        return ServerResponse.createBySuccessMsg("校验成功");
    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString();
        uuidStr = uuidStr.replace("-", "");
        return uuidStr;
    }
}
